extends Node2D

@onready var viewTexture = $ViewTexture
@onready var tetrisRegionViewport = $SubViewport
@onready var mainGameRegion = $SubViewport/TetrisRegion

var settingViewWidth = 180
var settingViewHeight = 320
var windowWidth
var windowHeight
var windowAspectRatio
var viewWidth
var viewHeight 

func _ready():
	MainGameSinglePlayerUi.hideExitGameToast()
	MainGameSinglePlayerUi.hideGameEndPanel()
	MainGameSinglePlayerUi.setVisible(true)
	
	GlobalSignals.MAIN_GAME_BUTTON_UPPER_TRIGGER.connect(MAIN_GAME_BUTTON_UPPER_TRIGGER_handler)
	GlobalSignals.MAIN_GAME_BUTTON_LEFT_TRIGGER.connect(MAIN_GAME_BUTTON_LEFT_TRIGGER_handler)
	GlobalSignals.MAIN_GAME_BUTTON_RIGHT_TRIGGER.connect(MAIN_GAME_BUTTON_RIGHT_TRIGGER_handler)
	GlobalSignals.MAIN_GAME_BUTTON_DOWN_TRIGGER.connect(MAIN_GAME_BUTTON_DOWN_TRIGGER_handler)
	GlobalSignals.SCORE_GETTED.connect(SCORE_GETTED_handler)
	GlobalSignals.GAME_END.connect(GAME_END_handler)
	GlobalSignals.GAME_CONTINUE.connect(GAME_CONTINUE_handler)
	
	viewTexture.texture = tetrisRegionViewport.get_texture()
	autoAdapt()
	MainGameButtons.setMode()
	
	Global.score = 0
	GlobalSignals.UPDATE_SCORE_VIEW.emit()
	
	mainGameRegion.startGame()
	
	SoundManager.playMusic(SoundManager.GAMING_BG)
	

func _exit_tree():
	SoundManager.stopAllMusic()
	
	MainGameButtons.visible = false
	MainGameSinglePlayerUi.setVisible(false)
	
	GlobalSignals.GAME_CONTINUE.disconnect(GAME_CONTINUE_handler)
	GlobalSignals.GAME_END.disconnect(GAME_END_handler)
	GlobalSignals.SCORE_GETTED.disconnect(SCORE_GETTED_handler)
	GlobalSignals.MAIN_GAME_BUTTON_UPPER_TRIGGER.disconnect(MAIN_GAME_BUTTON_UPPER_TRIGGER_handler)
	GlobalSignals.MAIN_GAME_BUTTON_LEFT_TRIGGER.disconnect(MAIN_GAME_BUTTON_LEFT_TRIGGER_handler)
	GlobalSignals.MAIN_GAME_BUTTON_RIGHT_TRIGGER.disconnect(MAIN_GAME_BUTTON_RIGHT_TRIGGER_handler)
	GlobalSignals.MAIN_GAME_BUTTON_DOWN_TRIGGER.disconnect(MAIN_GAME_BUTTON_DOWN_TRIGGER_handler)


func SCORE_GETTED_handler(data):
	Global.score += data
	GlobalSignals.UPDATE_SCORE_VIEW.emit()


func autoAdapt():
	var windowSize:Vector2i = DisplayServer.window_get_size()
	windowWidth = windowSize.x
	windowHeight = windowSize.y
	
	windowAspectRatio = windowWidth * 1.0 / windowHeight
	
	var settingViewAspectRatio = settingViewWidth * 1.0 / settingViewHeight
	if windowAspectRatio > settingViewAspectRatio:
		viewHeight = settingViewHeight
		viewWidth = windowWidth * viewHeight / windowHeight 
	else:
		viewWidth = settingViewWidth
		viewHeight = windowHeight * viewWidth / windowWidth
		
	var layout = Global.intelligentLayout(viewWidth, viewHeight, 1)[0]
	viewTexture.position = Vector2(layout["x"], layout["y"])
	viewTexture.scale = Vector2(layout["width"] * 1.0 / Global.widthRegion, layout["height"] * 1.0 / Global.heightRegion) 
	
	var debugMessage = "windowWidth: %s\nwindowHeight: %s\nwindowAspectRatio: %s\nviewWidth: %s\nviewHeight: %s\nosName: %s" % [windowWidth, windowHeight, windowAspectRatio, viewWidth, viewHeight, OS.get_name()]
	GlobalSignals.DEBUG_MESSAGE.emit(debugMessage)
	

func GAME_CONTINUE_handler():
	mainGameRegion.gameContinue()	


func GAME_END_handler():
	mainGameRegion.gameEnd()
	MainGameSinglePlayerUi.gameEnd()


func MAIN_GAME_BUTTON_UPPER_TRIGGER_handler():
	mainGameRegion.onButtonUpperTrigger()


func MAIN_GAME_BUTTON_LEFT_TRIGGER_handler():
	mainGameRegion.onRapidButtonLeftTrigger()


func MAIN_GAME_BUTTON_RIGHT_TRIGGER_handler():
	mainGameRegion.onRapidButtonRightTrigger()


func MAIN_GAME_BUTTON_DOWN_TRIGGER_handler():
	mainGameRegion.onRapidButtonDownTrigger()
	
	
